package com.xsg.printer.core;

import com.xsg.printer.core.enums.ColorEnum;
import com.xsg.printer.core.toolkit.ImageUtil;
import com.xsg.printer.core.toolkit.PathUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * 图片画板
 */
public class VoucherImageBoard extends VoucherBoard<Graphics2D, VoucherImagePaint>{

    private BufferedImage image;

    public VoucherImageBoard(int height, int width, int bgColor) {
        super(height, width, bgColor);
    }

    @Override
    public Graphics2D createContainer() {
        //新建图片
        image = new BufferedImage(getWidth() + 10, getHeight() + 10, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();
        //设置背景颜色
        graphics.setColor(ColorEnum.getColorByType(getBgColor()));
        //设置颜色区域大小
        graphics.fillRect(0, 0, getWidth() + 10, getHeight() + 10);
        //消除文本出现锯齿现象
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        return graphics;
    }

    @Override
    public VoucherImagePaint getPaint() {
        return new VoucherImagePaint(getContainer());
    }

    @Override
    public String getResult() {
        File file = new File(PathUtil.getRandomPngPath());
        ImageUtil.createImage(image, file);
        getContainer().dispose();
        return file.getPath();
    }
}
